package my.jwds.p98;

import my.jwds.p94.TreeNode;

public class IsValidBST {

    public static void main(String[] args) {
        IsValidBST obj = new IsValidBST();
        Integer [] nums = {2,1,3};
        TreeNode root = TreeNode.to_binaryHaveNull(nums);
        System.out.println(obj.isValidBST(root));
    }

    public boolean isValidBST(TreeNode root) {
        return dfs(root,Long.MIN_VALUE,Long.MAX_VALUE);
    }


    public boolean dfs(TreeNode root, long min,long max){
        if (root == null)return true;

        if (root.val >= max || root.val <= min){
            return false;
        }

        return dfs(root.left,min,root.val) && dfs(root.right,root.val,max);
    }


}